package edu.cth.speedappteachers;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CreateRoomActivity extends Activity {
	TextView tv1;
	private static final String baseUrl = "http://10.0.2.2/CIU196";
	View mainView;
	private String roomName = "";
	private String deviceUuid = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_room);
        mainView = getWindow().getCurrentFocus();
        
        // Android device ID
        DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this);
        deviceUuid = deviceUuidFactory.getDeviceUuid().toString();      
        
        
        // Checks if a user already exists in any room, if so then list them 
        //TODO
    }

    
	public void onClickCreateRoom(View v){
        Log.d("speedapp", "Button press registered");
        
        // Room input
        EditText room = (EditText) findViewById(R.id.roomName); 
        roomName = room.getText().toString();
        
        if(roomName.length() == 0) {
        	Toast.makeText(getApplicationContext(), "You need to enter a room name", Toast.LENGTH_SHORT).show();
        }

        // Try to join the room
        new createRoom().execute(baseUrl + "/createRoom.php?roomID=" + roomName + "&&androidID=" + deviceUuid);
    }
  
    /**
     * Sends a HTTP request for creating a new room 
     * */
    class createRoom extends AsyncTask<String, String, String> {
    	  @Override
      	  protected String doInBackground(String... uri) {
    		  HttpRequest r = new HttpRequest(uri);
    		  return r.getResponse();
    	  }

    	  @Override
    	    protected void onPostExecute(String result) {
    	        super.onPostExecute(result);
    	        //Do anything with response..

    	        tv1 = (TextView) findViewById(R.id.textdisplay01);
    	        tv1.setText(result);
 
    	        if(result.equals("Room created")) {
    	           	Intent intent = new Intent(mainView.getContext(), InRoomActivity.class);
    	    	    intent.putExtra("roomName", roomName);
    	    	    mainView.getContext().startActivity(intent);
    	    	    Toast.makeText(mainView.getContext(), "Room created", Toast.LENGTH_SHORT).show();
    	        } else if(result.equals("Room already exists")){
    	        	Toast.makeText(mainView.getContext(), "Room already exists", Toast.LENGTH_SHORT).show();
    	        } else {
    	        	Toast.makeText(mainView.getContext(), "Make sure connection works", Toast.LENGTH_SHORT).show();
    	        	Log.d("speedappteacher", "Error, can't create room! " + result);
    	        }
    	    }
	  }
 
}
